/**
 * 加载生成树数据
 */
jQuery(document).ready(function() {
    // 生成的树进行设置
    var setting = {
        view : {
            dblClickExpand : false,
            showLine : true,
            selectedMulti : false
        },
        check : {
            enable : true,
            chkStyle : "radio",
            radioType : "all"
        },
        data : {
            simpleData : {
                enable : true,
                idKey : "id",
                pIdKey : "pid",
                rootPId : ""
            }
        },
        callback : {
            beforeClick : function(treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj("tree");
                if (treeNode.isParent) {
                    zTree.expandNode(treeNode);
                    return false;
                } else {
                    demoIframe.attr("src", treeNode.file + ".html");
                    return true;
                }
            }
        }
    };
    var jsonData = eval($("#_json_id").val());

    var dataRoot = {
        "id" : 0,
        "name" : "根资源"
    };
    jsonData.push(dataRoot);

    var t = $("#tree");
    t = $.fn.zTree.init(t, setting, jsonData);
    demoIframe = $("#testIframe");
    demoIframe.bind("load", loadReady);
    var zTree = $.fn.zTree.getZTreeObj("tree");
    zTree.selectNode(zTree.getNodeByParam("id", 101));

    // 全部展开节点
    // var treeObj = $.fn.zTree.getZTreeObj("tree");
    // treeObj.expandAll(true);
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var nodes = treeObj.getNodes();
    if (nodes.length > 0) {
        treeObj.expandNode(nodes[0], true, false, true);
    }

    $("#add").bind("click", add);
    $("#edit").bind("click", edit);
    $("#delete").bind("click", deleted);

    var _tree_id = $("#_tree_id").val();
    if(_tree_id!='' && typeof(_tree_id)!="undefined") {
        var node = zTree.getNodeByParam("id", _tree_id);
        zTree.selectNode(node,true);//指定选中ID的节点
        zTree.expandNode(node, true, false);//指定选中ID节点展开
    }

});
/**
 * 加载树
 */
function loadReady() {
    var bodyH = demoIframe.contents().find("body").get(0).scrollHeight, htmlH = demoIframe
        .contents().find("html").get(0).scrollHeight, maxH = Math.max(
        bodyH, htmlH), minH = Math.min(bodyH, htmlH), h = demoIframe
        .height() >= maxH ? minH : maxH;
    if (h < 530)
        h = 530;
    demoIframe.height(h);
}
/**
 * 选择接点后进入添加界面
 *
 * @returns {Boolean}
 */
function add() {
    var nodes = $.fn.zTree.getZTreeObj("tree").getCheckedNodes(true);
    var treeNode = nodes[0];
    if (nodes.length == 0) {
        alert("请先选择一个父节点");
        return false;
    } else if (nodes.length > 0 && treeNode.type == 20) {
        alert("功能节点不能添加子资源");
        return false;
    } else {
        location.href = $("#add").attr("data") + "/" + treeNode.id;
    }
}

/**
 * 进入修改界面
 *
 * @returns {Boolean}
 */
function edit() {
    var nodes = $.fn.zTree.getZTreeObj("tree").getCheckedNodes(true);
    var treeNode = nodes[0];
    if (nodes.length == 0) {
        alert("请先选择一个节点");
        return false;
    } else if (treeNode.id === 0) {
        alert("根节点不能修改");
        return false;
    } else {
        location.href = $("#edit").attr("data") + "/" + treeNode.id;
    }
}
/**
 * 删除节点
 *
 * @returns {Boolean}
 */
function deleted() {
    var nodes = $.fn.zTree.getZTreeObj("tree").getCheckedNodes(true);
    var treeNode = nodes[0];
    if (nodes.length == 0) {
        alert("请先选择一个叶子节点");
        return false;
    } else if (typeof (treeNode.children) != 'undefined') {
        alert("请选择叶子节点删除");
        return false;
    } else {
        bootbox.confirm("确认删除?", function(result) {
            if(result) {
                location.href = $("#delete").attr("data") + "/" + treeNode.id;
            }
        });
    }
}
